//
//  ZSY_UserViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 15/12/28.
//  Copyright © 2015年 张诗宇. All rights reserved.
//

#import "ZSY_UserViewController.h"
#import "ZSY_UserView.h"
#import "ZSY_UserTableViewCell.h"
#import "ZSY_UserSettingViewController.h"
#import "ZSY_BecomeCHViewController.h"
#import "UMSocial.h"
#import "ZSY_HomeScrollViewDetailViewController.h"
#import "ZSY_CityHunterDetailViewController.h"
#import "ZSY_UserSettingCell.h"

@interface ZSY_UserViewController ()<UITableViewDataSource, UITableViewDelegate, ZSY_UserViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, retain) ZSY_BaseTableView *myTableView;
@property (nonatomic, retain) ZSY_UserView *userView;
@property(nonatomic,retain)ZSY_BaseImageView *imageView;
@property (nonatomic, retain) UMSocialAccountEntity *snsAccount;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation ZSY_UserViewController

- (void)dealloc {
    [_myTableView release];
    [_userView release];
    [super dealloc];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.allArray = [NSMutableArray array];
    self.allArray = [[ZSY_DataBaseManager shareDataBase] selectCity];
    
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    CGRect frame = self.view.frame;
    frame.size.height -= 80;
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    [self.myTableView registerClass:[ZSY_UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.separatorStyle = NO;
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"成为猎人" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-setting"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.userView = [[ZSY_UserView alloc] initWithFrame:CGRectMake(0, -300, WIDTH, 300)];
    self.userView.myDelegate = self;
    //    self.userPageV.alpha = 0.2;
//    self.userView.messageDelegate = self;
//    self.userView.orderDelegate = self;
//    self.userView.myWalletDelegate = self;


    //设置tableview的contentView距离上边界200
    //相对于0点,已经向下偏移了-200
    self.myTableView.contentInset = UIEdgeInsetsMake(300.0, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    
    //相对于0点,图片坐标应该是(0,-200)
    self.imageView = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake(0, -300.0, self.view.frame.size.width, 300)];
    self.imageView.image = [UIImage imageNamed:@"bundle_spot_edit_default_cover"];
    //设置imageView高度改变时宽度也跟着改变
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.myTableView addSubview:self.imageView];
    
    [self.myTableView addSubview:self.userView];
    [_imageView release];
    
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        ZSY_CityModel *model = [_allArray objectAtIndex:indexPath.row];
        cell.city = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ZSY_BaseView *view = [[[ZSY_BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)] autorelease];
    ZSY_BaseLabel *label = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake(5, 15, 100, 20)];
    label.text = @"游记&故事集";
    label.font = [UIFont systemFontOfSize:14];
    [view addSubview:label];
    [label release];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  200;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击后自动取消选中置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZSY_CityHunterDetailViewController *cityDetailVC = [[ZSY_CityHunterDetailViewController alloc] init];
    
    cityDetailVC.cityModel = [_allArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:cityDetailVC animated:YES];
    [cityDetailVC release];
    
}

#pragma mark - 删除收藏
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    self.allArray = [[DataBaseSingleton shareDataBase] selectMovie];
    ZSY_CityModel *city = [self.allArray objectAtIndex:indexPath.row];
    [[ZSY_DataBaseManager shareDataBase] deleteCityTable:city];
    
    [self.allArray removeObjectAtIndex:indexPath.row];
    
    
    NSLog(@"movie = %@", city.title);
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}


// scrollView协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    //    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    //    NSLog(@"y2 === %f",y);
    
    if (y < -300) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.imageView.frame = frame;
    }
    
}


// 头像点击协议方法
- (void)loginToSina {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            self.snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@ 微博url is %@",_snsAccount.userName,_snsAccount.usid,_snsAccount.accessToken,_snsAccount.iconURL, _snsAccount.profileURL);
            
            self.userView.name = _snsAccount.userName;
            self.userView.url = _snsAccount.iconURL;
            
            
        }});
    
//    [[UMSocialDataService defaultDataService] requestSnsFriends:UMShareToSina  completion:^(UMSocialResponseEntity *response){
//        NSLog(@"SnsFriends is %@",response.data);
//    }];


}

// 微博按钮协议方法实现
- (void)pushToweibo:(NSString *)url {
    ZSY_HomeScrollViewDetailViewController *vc = [[ZSY_HomeScrollViewDetailViewController alloc] init];
    vc.html_url = _snsAccount.profileURL;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}
// 更换背景点击协议实现
- (void)changeBackgrandImage {
    UIImagePickerController *pick = [[UIImagePickerController alloc] init];
    /*
     //选择类型 （照片库模式， 相机模式， 相机胶片模式）
     UIImagePickerControllerSourceTypePhotoLibrary：表示显示所有的照片
     UIImagePickerControllerSourceTypeCamera：表示从摄像头选取照片
     UIImagePickerControllerSourceTypeSavedPhotosAlbum：表示仅仅从相册中选取照片。
     allowEditing和allowsImageEditing 设置为YES，表示 允许用户编辑图片，否则，不允许用户编辑。
     */
    pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pick.allowsEditing = YES;//是否允许编辑
    pick.delegate = self;
    
    [self presentViewController:pick animated:YES completion:^{
        
        
    }];

}
#pragma mark -- UIImagePickerController协议方法
//选中相册中图片后会触发这个方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"info = %@",info);//打印是一个字典
    //根据字典中key为UIImagePickerControllerOriginalImage来获取图片
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//    self.userView.backImageView.image = image;
    self.imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //点击相册cancel触发的方法
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}



- (void)leftButtonAction:(UIBarButtonItem *)button {
    ZSY_BecomeCHViewController *vc = [[ZSY_BecomeCHViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

- (void)rightButtonAction:(UIBarButtonItem *)button {
    ZSY_UserSettingViewController *vc = [[ZSY_UserSettingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.name = _snsAccount.userName;
    vc.iconUrl = _snsAccount.iconURL;
    
    [vc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
